package com.aaa.system.controller;

import cn.hutool.core.util.RandomUtil;
import com.aaa.entity.UUserInfo;
import com.aaa.security.util.WebUtils;
import com.aaa.system.dao.UserInfoMapper;
import com.aaa.system.service.UserInfoService;
import com.aaa.util.SendSms;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/system/userInfo")

public class UserInfoController {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserInfoMapper userInfoMapper;




    @GetMapping("/getByName/{telephone}")
    public UUserInfo getByName(@PathVariable String telephone) {
        QueryWrapper<UUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("telephone", telephone);
        wrapper.eq("status", 0);
        UUserInfo one = userInfoService.getOne(wrapper);
        return one;
    }


    @GetMapping("/exit")
    public Result delCache() {
        String token = WebUtils.getRequest().getHeader("token");
        Boolean delete = redisTemplate.delete(token);
        System.out.println(delete);
        if (delete) {
            return new Result(2000,"清除缓存成功！");
        } else {
            return new Result(5000,"无缓存数据可清除！");
        }
    }


    @PostMapping("/getfreeze/{thelephone}")
    public UUserInfo getfreeze(@PathVariable String telephone) {
        UUserInfo freeze = userInfoService.freeze(telephone);
        return freeze;
    }

    @RequestMapping("sms/{mobile}")
    public Result sms(@PathVariable String mobile) {
        String s = SendSms.smsVerification(mobile);
        redisTemplate.opsForValue().set(s, "", 5, TimeUnit.MINUTES);
        System.out.println("s = " + s);
        return new Result(2000, "发送成功");
    }

    @RequestMapping("verify/{code}")
    public Result verify(@PathVariable String code) {
        Boolean aBoolean = redisTemplate.hasKey(code);
        if (aBoolean == true) {
            return new Result(2000, "验证成功");
        }
        return new Result(5000, "验证码错误");
    }

    @RequestMapping("/register/{mobile}/{qianPassword}")
    public Result register(@PathVariable String mobile, @PathVariable String qianPassword) {
        QueryWrapper<UUserInfo> Wrapper = new QueryWrapper<>();
        Wrapper.eq("telephone", mobile);
        UUserInfo uUserInfo = userInfoMapper.selectOne(Wrapper);
        if (uUserInfo == null) {
            UUserInfo uUserInfo1 = new UUserInfo();
            String encode = passwordEncoder.encode(qianPassword);
            //加密密码
            uUserInfo1.setPassword(encode);
            //生成用户名

            String s = RandomUtil.randomString(5);
            String name = ("用户" + s);
            uUserInfo1.setUname(name);
            uUserInfo1.setTelephone(mobile);
            //默认用户头像
            uUserInfo1.setUicon("https://img1.baidu.com/it/u=2121025603,1672671484&fm=253&fmt=auto&app=138&f=PNG?w=500&h=500");
            //用户注册时间
            uUserInfo1.setCreatetime(LocalDateTime.now());
            uUserInfo1.setStatus(0);

            Result register = userInfoService.register(uUserInfo1);
            return register;
        }
        return new Result(4000, "账号已存在");

    }



}
